<template>
	<div class="boxStyle">
		<el-row type="flex" class="row-bg" justify="start" style="margin-bottom: 10px;">
			<el-col :span="1">
				<el-button type="warning" size="small" v-if="addShow!==-1" @click="add()" icon="el-icon-plus">新增</el-button>
			</el-col>
			<el-col :span="1" style="margin-left: 20px;margin-top: 5px; ">手机号 :</el-col>
			<el-col :span="3">
				<el-input v-model="mobile" size="small"></el-input>
			</el-col>
			<el-col :span="1" style="margin-left: 20px;margin-top: 5px; ">渠道商 :</el-col>
			<el-col :span="3">
				<el-select v-model="channelVal" placeholder="请选择" size="small" style="width: 180px;">
					<el-option value="" label="全部"></el-option>
					<el-option v-for="item in channelList" :key="item.id" :label="item.name" :value="item.id">
					</el-option>
				</el-select>
			</el-col>
			<el-col :span="1" style="margin-left:20px;">
				<el-button type="warning" size="small" @click="search" icon="el-icon-search">查找</el-button>
			</el-col>
		</el-row>
		<el-table ref="multipleTable" stripe border :data="writeoffLists" tooltip-effect="dark" style="margin-bottom: 10px;width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="序号" width="55"></el-table-column>
			<el-table-column prop="name" label="核销员姓名" width="120"></el-table-column>
			<el-table-column prop="mobile" label="核销员手机" width="150"></el-table-column>
			<el-table-column prop="createTime" label="创建时间" width="200" :formatter="dateFormat"></el-table-column>
			<el-table-column prop="channelName" label="所属渠道商" width="120"></el-table-column>
			<el-table-column prop="remark" label="备注" width="120"></el-table-column>
			<el-table-column label="操作" min-width="300">
				<template slot-scope="scope">
					<el-button size="mini" type="primary" v-if="updateShow!==-1" @click="update(scope.$index,scope.row.id)">编辑</el-button>
					<el-button size="mini" type="danger" @click="del(scope.row.id)" v-if="deleShow!==-1">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination background @current-change="handleCurrentChange" :current-page="currentPage" :page-size="size" layout="prev, pager, next,total, slot" :total="totals">
			<span style="margin-top: -2px;color: #606266;font-weight: 100;">第<el-input type="text" v-model="inputPage" style="width: 60px;margin: 0 5px;"/>页<el-button size="mini" style="background: #409EFF; color: #fff;margin:0 5px;" @click="handleCurrentChange(inputPage)">跳转</el-button><span>总计{{totalPage}}页</span></span>
		</el-pagination>
		<div class="modal fade" id="writeoffAdd" tabindex="-1" role="dialog" data-keyboard="false">
			<div class="modal-dialog" role="document">
				<div class="modal-content" style="width:800px;height: 540px;">
					<div class="modal-header" style="border-radius: 4px;">
						<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
						<ul class="nav nav-tabs" role="tablist">
							<li role="presentation" class="active">
								<a href="#qusInfo" aria-controls="qusInfo" role="tab" data-toggle="tab">新增/编辑</a>
							</li>
						</ul>
					</div>
					<div class="modal-body" style="padding-right: 60px;height:410px;overflow: hidden;overflow-y: auto;">
						<div class="tab-content">
							<div role="tabpanel" class="tab-pane active" id="qusInfo">
								<el-form :model="writeoffModel" ref="writeoffModel" label-width="100px" class="demo-ruleForm" @submit.native.prevent>
									<input type="hidden" name="id" v-model="id">
									<el-col :span="24">
										<el-form-item label="核销员手机:" prop="mobile" :rules="PHONE">
											<el-input v-model="writeoffModel.mobile" size="small" style="width:42%;"></el-input>
											<span style="line-height: 2;">核销员需先注册为商城账号</span>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="姓名 :" prop="name" :rules="MUSTINFO">
											<el-input v-model="writeoffModel.name" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="备注:" prop="remark">
											<el-input v-model="writeoffModel.remark" size="small"></el-input>
										</el-form-item>
									</el-col>
									<el-col :span="12">
										<el-form-item label="所属渠道商:" prop="channelId" :rules="MUSTINFO">
											<el-select v-model="writeoffModel.channelId" placeholder="请选择" size="small" style="width: 180px;">
												<el-option v-for="item in channelList" :key="item.id" :label="item.name" :value="item.id">
												</el-option>
											</el-select>
										</el-form-item>
									</el-col>
								</el-form>
							</div>
						</div>
					</div>
					<div class="modal-footer">
						<el-button type="success" size="small" id="save" @click="save('writeoffModel')" :loading="loading">保存</el-button>
						<el-button type="success" size="small" id="usave" @click="uSave('writeoffModel')" :loading="loading">保存</el-button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
				</div>
			</div>
			<div></div>
		</div>
	</div>
</template>
<script>
	import axios from "axios"
	import { formatDate } from 'static/js/date.js'
	import qs from "qs"
	import { MUSTNUMBER, MUSTINFO, PHONE } from 'static/js/rules.js'
	export default {
		data() {
			return {
				purl: this.$store.state.purl, //权限数组
				url: this.GLOBAL.url,
				imgurl: this.GLOBAL.imgurl,
				currentPage: 1, //当前页
				totals: 0, //分页总条数
				size: 0, //每页条数
				totalPage: 0, //总页数
				inputPage: 1,
				writeoffLists: [],
				addShow: 0,
				MUSTNUMBER,
				MUSTINFO,
				PHONE,
				loading: false,
				updateShow: 0,
				deleShow: 0,
				id: '',
				channelVal: '',
				mobile: '',
				channelList: [],
				writeoffModel: {
					name: '',
					remark: '',
					mobile: '',
					channelId: ''
				},
			}
		},
		activated() {
			this.load(1);
			this.channelVal = '';
			this.mobile = '';
			this.deleShow = this.purl[0].findIndex((val) => val === "/interface/writeoff/add")
			this.addShow = this.purl[0].findIndex((val) => val === "/interface/writeoff/add") //判断是否拥有增加权限
			this.updateShow = this.purl[0].findIndex((val) => val === "/interface/writeoff/view") //判断是否拥有更新权限
		},
		methods: {
			load(num) {
				axios.get("/interface/writeoff/list?pageNo=" + num).then((res) => {
					this.writeoffLists = res.data.data.list;
					this.channelList = res.data.data.slist;
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})

			},
			add() {
				$("#writeoffAdd").modal('show');
				$("#save").show();
				$("#usave").hide();
				this.writeoffModel = {
					name: '',
					remark: '',
					mobile: '',
					channelId: ''
				}
			},
			save(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						this.loading = true;
						axios({
							method: 'post',
							url: '/interface/writeoff/add',
							data: this.writeoffModel,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.loading = false;
								$("#writeoffAdd").modal("hide");
								this.load(this.currentPage);
								this.$message.success("新增成功")
							} else {
								this.loading = false;
								var msg = res.data.msg;
								$("#writeoffAdd").modal("hide");
								this.$alert(msg, '提示消息', {
									confirmButtonText: '确定',
									callback: action => {
										$("#writeoffAdd").modal("show");
									}
								});
							}
						});
					}
				})
			},
			update(index, id) {
				$("#writeoffAdd").modal('show');
				$("#save").hide();
				$("#usave").show();
				this.id = id;
				axios.get("/interface/writeoff/view?id=" + id).then((res) => {
					this.writeoffModel = res.data.data.writeoff
				})
			},
			uSave(formname) {
				this.$refs[formname].validate((valid) => {
					if(valid) {
						this.writeoffModel.id = this.id;
						this.loading = true;
						axios({
							method: 'post',
							url: '/interface/writeoff/add',
							data: this.writeoffModel,
							headers: {
								'Content-Type': 'application/json'
							}
						}).then((res) => {
							if(res.data.code == 1) {
								this.loading = false;
								$("#writeoffAdd").modal("hide");
								this.load(this.currentPage);
								this.$message.success("编辑成功")
							} else {
								this.loading = false;
								var msg = res.data.msg;
								$("#writeoffAdd").modal("hide");
								this.$alert(msg, '提示消息', {
									confirmButtonText: '确定',
									callback: action => {
										$("#writeoffAdd").modal("show");
									}
								});
							}
						});
					}
				})
			},
			handleCurrentChange(val) {
				this.currentPage = val - 0;
				this.inputPage = val;
				this.load(val)
			},
			search() {
				axios.post("/interface/writeoff/list?", qs.stringify({
					channelId: this.channelVal,
					mobile: this.mobile,
					pageNo: 1
				})).then((res) => {
					this.writeoffLists = res.data.data.list;
					this.channelList = res.data.data.slist;
					this.totals = res.data.data.pageBean.totalRow; //总条数
					this.size = res.data.data.pageBean.pageSize; //每页条数
					this.totalPage = res.data.data.pageBean.totalPage; //总页数
				})
			},
			dateFormat: function(row, column) {
				var date
				if(row[column.property]) {
					date = new Date(parseInt(row[column.property]))
					date = formatDate(date, 'yyyy-MM-dd hh:mm:ss')
				}
				if(date == undefined) {
					return "";
				}
				return date;
			},
			del(id) {
				this.$confirm('确认要删除此条数据吗?', '删除警告', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					axios({
						method: 'post',
						url: '/interface/writeoff/add',
						data: {
							"id": id,
							"isDel": 1
						},
						headers: {
							'Content-Type': 'application/json'
						}
					}).then((res) => {
						if(res.data.code == 1) {
							this.load(this.currentPage);
							this.$message({
								type: 'success',
								message: '删除成功!'
							});
						} else {
							var msg = res.data.msg;
							this.$alert(msg, '提示消息', {
								confirmButtonText: '确定',
								callback: action => {}
							});
						}
					})

				}).catch(() => {});
			},
		}
	}
</script>

<style>
	#writeoffAdd .modal-dialog {
		left: 35% !important;
		top: 5% !important;
	}
	
	#oneP .el-form-item__label {
		width: 103px !important;
	}
	
	#oneP .el-form-item__content {
		margin-left: 103px !important;
	}
	
	#mobile .el-form-item__label {
		width: 130px !important;
	}
	
	#mobile .el-form-item__content {
		margin-left: 130px !important;
	}
</style>